package com.beagle.njddapp.core;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.beagle.njddapp.base.OilApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.HandleTick;
import com.beagle.njddapp.bean.oil.HandleBean;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.event.OilOrderEvent;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-22 15:44
 */
public class OilOrderService extends Service {


    private OilApiService apiService;

    private Handler handler;
    private  double lat=0,lnt=0;

    @Override
    public void onCreate() {
        super.onCreate();
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        apiService = retrofit.create(OilApiService.class);
        handler=new Handler(getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                getOilOrder();
            }
        };
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (handler!=null){
            handler.sendEmptyMessageDelayed(1,1000);
        }else {
            retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
            //获取接口对象
            apiService = retrofit.create(OilApiService.class);
            handler=new Handler(getMainLooper()){
                @Override
                public void handleMessage(@NonNull Message msg) {
                    super.handleMessage(msg);
                    getOilOrder();
                }
            };
            handler.sendEmptyMessageDelayed(1,1000);
        }
        return super.onStartCommand(intent, flags, startId);
    }



    public void getOilOrder() {
        if(TextUtils.isEmpty(UserService.getInstance().getToken())){
            handler.sendEmptyMessageDelayed(1,5000);
            return;
        }
        if(lat==0){
            lat=LocationManager.getInstance().getLatitude();
            lnt=LocationManager.getInstance().getLongitude();
        }
        double realLat,realLnt;
        realLat=LocationManager.getInstance().getLatitude();
        realLnt=LocationManager.getInstance().getLongitude();
        if(realLat==0){
            realLat=lat;
        }else {
            lat=realLat;
        }
        if(realLnt==0){
            realLnt=lnt;
        }else {
            lnt=realLnt;
        }
        if(realLat==0||realLnt==0){
            handler.sendEmptyMessageDelayed(1,5000);
            return;
        }
        HandleTick  handleTick=new HandleTick();
        handleTick.setLatitude(realLat);
        handleTick.setLongitude(realLnt);
        handleTick.setRequestTime(System.currentTimeMillis()/1000);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), new Gson().toJson(handleTick));
        apiService.handleTick(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<OilHomeBean>>() {

            @Override
            public void success(BaseOilHttpListResponse<OilHomeBean> oilOrderBeanBaseOilHttpListResponse) {
                LogUtils.d("success");
                if (oilOrderBeanBaseOilHttpListResponse!=null&&oilOrderBeanBaseOilHttpListResponse.results!=null){
                        EventBus.getDefault().post(new OilOrderEvent(oilOrderBeanBaseOilHttpListResponse.results));

                }else {
                    EventBus.getDefault().post(new OilOrderEvent(new ArrayList<>()));

                }
                handler.sendEmptyMessageDelayed(1,5000);
            }

            @Override
            public void error(int code, String message) {
                handler.sendEmptyMessageDelayed(1,5000);

            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
